<?php

namespace app\modules\user\controllers;

use app\models\SubUser;
use app\models\UserLog;
use app\models\Util;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;

/**
 * 子账号管理
 * Class UserController
 * @package app\modules\user\controllers
 */
class UserController extends BaseController
{
    /**
     * 用户列表
     * @return string
     */
    public function actionList()
    {
        $query = SubUser::find();
        $query->andWhere(['uid' => $this->user->id]);
        $pagination = new Pagination(['totalCount' => $query->count()]);
        $query->orderBy('id DESC');
        $query->offset($pagination->offset)->limit($pagination->limit);
        $userList = $query->all();
        return $this->render('list', [
            'userList' => $userList,
            'pagination' => $pagination,
        ]);
    }

    /**
     * 添加修改用户
     * @return string
     * @throws NotFoundHttpException
     * @throws \yii\base\Exception
     */
    public function actionEdit()
    {
        $id = $this->get('id');
        if (!empty($id)) {
            $user = SubUser::findOne(['id' => $id, 'uid' => $this->user->id]);
            if (empty($user)) {
                throw new NotFoundHttpException('没有找到用户信息。');
            }
        } else {
            $user = new SubUser();
            $user->uid = $this->user->id;
            $user->status = SubUser::STATUS_OK;
            $user->create_time = time();
        }
        if ($user->load($this->post())) {
            if (!empty($user->password) && !preg_match('/^\$2y/', $user->password)) {
                $user->password = Yii::$app->security->generatePasswordHash($user->password);
            }
            if ($user->save()) {
                UserLog::info($this->user->id, '保存子用户', print_r($user->attributes, true));
                Yii::$app->session->addFlash('success', '数据已保存。');
                Yii::$app->session->setFlash('redirect', json_encode([
                    'url' => Url::to(['/user/user/list']),
                    'txt' => '用户列表'
                ]));
            }
        }
        return $this->render('edit', [
            'user' => $user,
        ]);
    }

    /**
     * 用户详情
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionView()
    {
        $id = $this->get('id');
        $user = SubUser::findOne(['id' => $id, 'uid' => $this->user->id]);
        if (empty($user)) {
            throw new NotFoundHttpException('没有找到用户信息。');
        }
        return $this->render('view', [
            'user' => $user,
        ]);
    }

    /**
     * 重置用户密码AJAX接口
     * @return array
     * @throws \yii\base\Exception
     */
    public function actionResetPassword()
    {
        $id = $this->get('id');
        $user = SubUser::findOne(['id' => $id, 'uid' => $this->user->id]);
        if (empty($user)) {
            return ['error' => 'SERVER', 'message' => '没有找到用户。'];
        }
        $password = Util::randomStr(6, 1);
        $user->password = Yii::$app->security->generatePasswordHash($password);
        $user->save(false);
        return ['new_password' => $password];
    }
}
